package com.naihe.anderson.sms.activity;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.InputType;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.naihe.anderson.sms.R;
import com.naihe.anderson.sms.application.NhSmsApp;
import com.naihe.anderson.sms.entity.Note;

public class NoteCreateActivity extends Activity implements OnClickListener {

	EditText note_title;
	EditText note_body;
	Note note;
	Date dateTime = new Date();
	NhSmsApp nhSmsApp;
	
	public static final int MSG_WHAT_MENU_COLLECTION_SESSION = 0;
	
	Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_WHAT_MENU_COLLECTION_SESSION://加密
				if(mPopupWindow == null)
					initPopupWindows();
				
				mPopupWindow.update();
				mPopupWindow.showAtLocation(findViewById(R.id.note_body), Gravity.CENTER, 0, 0);
				break;
			default:
				break;
			}
		}
		
	};
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.create_note_layout);
		
		nhSmsApp = (NhSmsApp) getApplication();
		nhSmsApp.setActivity(this);
		note_title = (EditText) findViewById(R.id.note_title);
		note_body = (EditText) findViewById(R.id.note_body);
		
		note = (Note) getIntent().getSerializableExtra("note");
		if(note != null){
			note_title.setText(note.title);
			if(!TextUtils.isEmpty(note.cipher)){
	        	mHandler.sendEmptyMessageDelayed(MSG_WHAT_MENU_COLLECTION_SESSION, 10);
	        } else {
	        	note_body.setText(note.body);
	        }
		} else {
			note_title.setText(new SimpleDateFormat("yyyy年MM月dd日").format(dateTime));
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// 截获按键事件
		if (KeyEvent.KEYCODE_BACK == keyCode) {
			if(mPopupWindow != null && mPopupWindow.isShowing()){
				mPopupWindow.dismiss();
			} else {
				backData();
			}
			nhSmsApp.setActivity(null);
			NoteCreateActivity.this.finish();
			return true;
		}

		return super.onKeyDown(keyCode, event);
	}
	
	PopupWindow mPopupWindow = null;
	EditText editText;
	private void initPopupWindows(){
		LayoutInflater mLayoutInflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);  
        View popunwindwow = mLayoutInflater.inflate( R.layout.popup_edit_layout, null);
        editText = (EditText) popunwindwow.findViewById(R.id.baseSignature_edit);
        Button checkbtn_cancel =(Button) popunwindwow.findViewById(R.id.checkbtn_cancel);
        Button checkbtn_ok =(Button) popunwindwow.findViewById(R.id.checkbtn_ok);
        TextView title_text = (TextView) popunwindwow.findViewById(R.id.title_text);
       
        editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        editText.setHint(getResources().getString(R.string.save_key_word));
        
        title_text.setText(getResources().getString(R.string.note_mima_see));
        checkbtn_cancel.setOnClickListener(NoteCreateActivity.this);
        checkbtn_ok.setOnClickListener(NoteCreateActivity.this);
        
        mPopupWindow = new PopupWindow(popunwindwow, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 
        mPopupWindow.setOutsideTouchable(false);  
        mPopupWindow.setFocusable(true);
	}
	
	private void backData(){
		String title = note_title.getText().toString();
		String body = note_body.getText().toString();
		if(TextUtils.isEmpty(title) && TextUtils.isEmpty(body)) return;
		
		if(title.equals(new SimpleDateFormat("yyyy年MM月dd日").format(dateTime)) && TextUtils.isEmpty(body)) return;
		
		if(note == null){//新建
			Note creatNote = new Note();
			creatNote.title = title;
			creatNote.body = body;
			creatNote.id = dateTime.getTime();
			creatNote.createTime = dateTime.getTime();
			creatNote.cipher = "";
			
			Intent intent = new Intent(NoteCreateActivity.this, NoteActivity.class);
			intent.putExtra("type", "create");
			intent.putExtra("note", creatNote);
			NoteCreateActivity.this.setResult(RESULT_OK, intent);
		} else {
			if(!note.title.equals(title) || !note.body.equals(body)){
				note.title = title;
				note.body = body;
				note.createTime = dateTime.getTime();
				
				Intent intent = new Intent(NoteCreateActivity.this, NoteActivity.class);
				intent.putExtra("type", "update");
				intent.putExtra("note", note);
				NoteCreateActivity.this.setResult(RESULT_OK, intent);
			}
		}
	}

	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.checkbtn_cancel){
			mPopupWindow.dismiss();
			nhSmsApp.setActivity(null);
			NoteCreateActivity.this.finish();
		} else if(v.getId() == R.id.checkbtn_ok){
			String signature = editText.getText().toString().trim();
			if(note.cipher.equals(signature)){
				note_body.setText(note.body);
				mPopupWindow.dismiss();
			} else {
				editText.setText("");
				Toast.makeText(NoteCreateActivity.this, "密码错误", Toast.LENGTH_SHORT).show();
			}
		}
	}
}
